﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC.AuxiliaryCurve
{
	public class BoostFunction
	{
		public static Face GetFaceofPoint(ExternalCommandData cmdData, XYZ pointOnFace, Element elementArgu)
		{
			Face result = null;
			GeometryElement geomElem = elementArgu.get_Geometry(new Options
			{
				View = cmdData.Application.ActiveUIDocument.ActiveView
			});
			Solid solid = null;
			BoostFunction.AddSolids(geomElem, ref solid);
			List<FaceArray> list = new List<FaceArray>();
			FaceArray faces = solid.Faces;
			list.Add(faces);
			FaceArray faceArray = new FaceArray();
			foreach (FaceArray faceArray2 in list)
			{
				foreach (object obj in faceArray2)
				{
					Face face = (Face)obj;
					faceArray.Append(face);
				}
			}
			foreach (object obj2 in faceArray)
			{
				Face face2 = (Face)obj2;
				IntersectionResult intersectionResult = face2.Project(pointOnFace);
				if (intersectionResult != null && intersectionResult.XYZPoint.DistanceTo(pointOnFace) < Math.Pow(10.0, -3.0))
				{
					result = face2;
				}
			}
			return result;
		}

		public static void AddSolids(GeometryElement geomElem, ref Solid solids)
		{
			foreach (GeometryObject geometryObject in geomElem)
			{
				Solid solid = geometryObject as Solid;
				if (null != solid)
				{
					solids = solid;
				}
				else
				{
					GeometryInstance geometryInstance = geometryObject as GeometryInstance;
					if (null != geometryInstance)
					{
						BoostFunction.AddSolids(geometryInstance.GetInstanceGeometry(geometryInstance.Transform), ref solids);
					}
				}
			}
		}

		public static bool ConvertUVToXYZ(Dictionary<UV, UV> dicArgu, Face faceArgu, ref Dictionary<XYZ, XYZ> dicOut)
		{
			if (dicArgu == null || faceArgu == null || dicOut == null)
			{
				return false;
			}
			foreach (KeyValuePair<UV, UV> keyValuePair in dicArgu)
			{
				dicOut.Add(faceArgu.Evaluate(keyValuePair.Key), faceArgu.Evaluate(keyValuePair.Value));
			}
			return true;
		}
	}
}
